var express = require('express');
var router = express.Router();

const sql = require("../sql/sql");
const search = require("../sql/search");
const uuid = require("node-uuid")
const tools = require("../tools")

/**
* @apiGroup 获取搜索历史
* @api {get} /api/search 获取搜索历史关键字
* @apiVersion 0.0.0
* @apiDescription 根据userId 获取搜索历史关键字数据
* @apiParam {String} userId 接收用户登录之后的id
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccess {object} data 请求数据
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"请求成功",
*    "data":[{},{},...]
* }
* @apiSampleRequest http://localhost:3000/api/search
*/

// 获取搜索关键字
router.get('/', function(req, res, next) {
    let {userId} = req.query;
    let token = req.headers.token;

    tools.verify(token).then(response=>{
        sql.sort(search, {userId},{_id:0,__v:0},{time:-1}).then(data=>{
            res.send({
                state:200,
                title:"请求成功",
                data
            })
        })
    }).catch(err=>{
        res.send({
            state: 10086,
            title: "登录失效"
        })
    })
});


/**
* @apiGroup 添加搜索历史
* @api {post} /api/search/add 添加搜索历史关键字
* @apiVersion 0.0.0
* @apiDescription 根据 userId 和 搜索关键字 添加搜索历史关键字数据
* @apiParam {String} userId 接收用户登录之后的id
* @apiParam {String} searchText 接收要搜索的关键字
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"添加成功"
* }
* @apiSampleRequest http://localhost:3000/api/search/add
*/

// 设置搜索关键字
router.post('/add', function(req, res, next) {
    let {userId, searchText} = req.body;
    let token = req.headers.token;

    tools.verify(token).then(response=>{
        sql.insert(search, {
            searchId:"search-"+uuid.v1(),
            userId,
            searchText,
            time:Date.now()
        }).then(()=>{
            res.send({
                state:200,
                title:"设置成功"
            })
        })
    }).catch(err=>{
        res.send({
            state: 10086,
            title: "登录失效"
        })
    })
});

/**
* @apiGroup 删除搜索历史
* @api {post} /api/search/del 删除搜索历史关键字
* @apiVersion 0.0.0
* @apiDescription 根据 userId 和 搜索关键字id 删除搜索历史关键字数据
* @apiParam {String} userId 接收用户登录之后的id
* @apiParam {String} searchId 接收要删除的关键字的id
* @apiSuccess {number} state 请求状态
* @apiSuccess {string} title 请求说明
* @apiSuccessExample {json} Success-Response:
* {
*    "state":200,
*    "title":"删除成功"
* }
* @apiSampleRequest http://localhost:3000/api/search/del
*/

// 删除搜索关键字
router.post('/del', function(req, res, next) {
    let {userId, searchId} = req.body;
    let token = req.headers.token;

    tools.verify(token).then(response=>{
        sql.delete(search, {userId, searchId}).then(()=>{
            res.send({
                state:200,
                title:"删除成功"
            })
        })
    }).catch(err=>{
        res.send({
            state: 10086,
            title: "登录失效"
        })
    })
});

module.exports = router;
